<?php

namespace App\Models;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class Banner extends BaseModel
{

    protected $fillable = [
        'title',
        'image',
        'link',
        'status',
        'sort',
    ];

    /**
     * 图片地址
     */
    protected function image(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => asset($value),
            set: fn ($value) => normalizePath($value)
        );
    }

    /**
     * 获取列表数据
     * @param array $params
     * @return LengthAwarePaginator
     */
    public static function getQueryData($params = [])
    {
        return self::query()->when($params['title'] ?? '',function($query) use ($params){
            return $query->where('title','like','%'.$params['title'].'%');
        })->when($params['status'] ?? '',function($query,$value){
            return $query->where('status',$value);
        })->latest('id')->paginate(request('pageSize',20));
    }
}
